package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.Team;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.service.TeamService;
import org.javaweb.cms_web.service.UserService;
import org.javaweb.cms_web.util.HostHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TeamManagementController {
    @Resource
    private HostHolder hostHolder;
    @Resource
    private TeamService teamService;
    @Resource
    private UserService userService;

    //跳转到团队基本信息维护界面
    @GetMapping("/admin/teamadmin/teaminfo")
    public String redirectToTeamInfo(Model model) {
        User user = hostHolder.getUser();
        Integer teamId = user.getTeamId();
        Team team = teamService.getTeamById(teamId);
        List<User> members = teamService.getTeamMembersDetail(teamId);
        model.addAttribute("team", team);
        model.addAttribute("members", members);
        return "/admin/teaminfo";  // 跳转到团队基本信息维护页面
    }

    //修改团队基本信息
    @PostMapping("/admin/teamadmin/teaminfo/edit")
    public String editTeamInfo(@ModelAttribute Team team) {
        teamService.updateTeamInfo(team);
        return "redirect:/admin/teaminfo";  // 跳转到团队基本信息维护页面
    }

    //跳转到团队成员管理界面
    @GetMapping("/admin/teamadmin/teammembers")
    public String redirectToTeamMembers(Model model) {
        User user = hostHolder.getUser();
        Integer teamId = user.getTeamId();
        List<User> members = teamService.getTeamMembersDetail(teamId);
        model.addAttribute("teamId", teamId);
        model.addAttribute("members", members);
        return "/admin/teammembers";  // 跳转到团队成员管理页面
    }

    //跳转到团队成员信息修改界面
    @GetMapping("/admin/teamadmin/teammembers/edit/{teamId}")
    public String redirectToNewTeamMember(@PathVariable("teamId") Integer teamId, Integer userId, Model model) {
        if (userId != null) {
            User user = userService.findUserById(userId);
            model.addAttribute("user", user);
        }
        model.addAttribute("teamId", teamId);
        return "/admin/edit-teammembers";
    }

    //新建团队成员
    @PostMapping("/admin/teamadmin/teammembers/new")
    public String addTeamMember(@ModelAttribute User user, Model model) {
        User user1 = hostHolder.getUser();
        user.setTeamId(user1.getTeamId());
        if (userService.addUser(user)) {
            model.addAttribute("msg", "添加成功");
        } else {
            model.addAttribute("errorMsg", "添加失败");
        }
        return "redirect:/admin/teamadmin/teammembers";  // 跳转到团队成员管理页面
    }

    //修改团队成员信息
    @PostMapping("/admin/teamadmin/teammembers/edit")
    public String editTeamMember(@ModelAttribute User user, Model model) {
        if (userService.updateUserSomeFiled(user)) {
            model.addAttribute("msg", "修改成功");
        } else {
            model.addAttribute("errorMsg", "修改失败");
        }
        return "redirect:/admin/teamadmin/teammembers";  // 跳转到团队成员管理页面
    }

    //跳转到团队成员信息审核页面
    @GetMapping("/admin/teamadmin/teammembers/review")
    public String redirectToTeamMembersReview(Model model) {
        User user = hostHolder.getUser();
        List<User> users = userService.getUsersForReview(user.getTeamId());
        model.addAttribute("users", users);
        return "/admin/teammembers-review";  // 跳转到团队成员信息审核页面
    }

    //审核团队成员信息
    @PostMapping("/admin/teamadmin/teammembers/review")
    public String reviewTeamMember(int userId, int status, Model model) {
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
        if (userService.updateUserSomeFiled(user)) {
            model.addAttribute("msg", "审核成功");
        } else {
            model.addAttribute("errorMsg", "审核失败");
        }
        return "redirect:/admin/teamadmin/teammembers/review";  // 跳转到团队成员信息审核页面
    }

}
